<?php

/**
 * This is the model class for table "paylist".
 *
 * The followings are the available columns in table 'paylist':
 * @property string $id
 * @property string $name
 * @property string $note
 * @property string $manager_id
 * @property string $date_from
 * @property string $date_to
 * @property string $price_total
 * @property string $price_return
 * @property string $created
 *
 * The followings are the available model relations:
 * @property Manager $manager
 * @property Bill[] $bills
 */
class Paylist extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'paylist';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, manager_id, date_from, date_to, price_total, price_return, created', 'required'),
			array('name', 'length', 'max'=>255),
			array('manager_id, price_total, price_return', 'length', 'max'=>10),
			array('note', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name, note, manager_id, date_from, date_to, price_total, price_return, created', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'manager' => array(self::BELONGS_TO, 'Manager', 'manager_id'),
			'bills' => array(self::MANY_MANY, 'Bill', 'paylist_has_bill(paylist_id, bill_id)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Tên Paylist',
			'note' => 'Ghi chú',
			'manager_id' => 'Quản lý tạo',
			'date_from' => 'Đơn hàng từ ngày',
			'date_to' => 'Đơn hàng đến ngày',
			'price_total' => 'Tổng tiền',
			'price_return' => 'Tiền trả cho đối tác',
			'created' => 'Ngày tạo',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('note',$this->note,true);
		$criteria->compare('manager_id',$this->manager_id,true);
		$criteria->compare('date_from',$this->date_from,true);
		$criteria->compare('date_to',$this->date_to,true);
		$criteria->compare('price_total',$this->price_total,true);
		$criteria->compare('price_return',$this->price_return,true);
		$criteria->compare('created',$this->created,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Paylist the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    
    public function getUrl(){
        return Yii::app()->createUrl('/admin/bill/paylistView', array('id' => $this->id));
    }
}
